import crypto from 'crypto'
// import JSEncrypt from 'jsencrypt/bin/jsencrypt'
// import storage from './storage.js'
export default {
  // 以下是公共方法
  // 登录密码加密
  pwdEncrypt: function(pwd) {
    const pubKey =
            'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxG/qXHBlmcBtM2YNyxFVMPyMLEMN2nYs8x5ygukufzgLyTgPbteGaNiGcN4Ddo50VeCY4ONglljMEtbAyAOmxRHs1feBH2yHmxE2XKRlylJO1Bg4H4dU/1gId3PtbaoQIqZo4gJXT3sxBKOxWorQFd05DZMkeu92SFxeCsDSnGtpIk3bLAL9v+sslYlF+GFR/ZpvIkA3XLfcpFv1N9gpdl08pFR+KBTnVvKn7tUE8q9L+howU8IH2AIrvpgZfgIWEymyznDG+GLhW1fiSm6wqaIhABF0x6Ob9ZZytnR03LhprQ2+NEl5dNpdHRtVeyKsOnO7j70WUvV74ANgHMYwSwIDAQAB'
    const rsaWithPublicKey = new JSEncrypt()
    rsaWithPublicKey.setPublicKey(pubKey) // 设置 加密公钥
    const pwdDigest = crypto
      .createHash('sha256')
      .update(pwd)
      .digest('hex')
    const data = rsaWithPublicKey.encrypt(pwdDigest) // 进行加密
    return data
  },
  skEncrypt: function(pwd) {
    const pubKey =
            'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxG/qXHBlmcBtM2YNyxFVMPyMLEMN2nYs8x5ygukufzgLyTgPbteGaNiGcN4Ddo50VeCY4ONglljMEtbAyAOmxRHs1feBH2yHmxE2XKRlylJO1Bg4H4dU/1gId3PtbaoQIqZo4gJXT3sxBKOxWorQFd05DZMkeu92SFxeCsDSnGtpIk3bLAL9v+sslYlF+GFR/ZpvIkA3XLfcpFv1N9gpdl08pFR+KBTnVvKn7tUE8q9L+howU8IH2AIrvpgZfgIWEymyznDG+GLhW1fiSm6wqaIhABF0x6Ob9ZZytnR03LhprQ2+NEl5dNpdHRtVeyKsOnO7j70WUvV74ANgHMYwSwIDAQAB'
    const rsaWithPublicKey = new JSEncrypt()
    rsaWithPublicKey.setPublicKey(pubKey) // 设置 加密公钥
    const data = rsaWithPublicKey.encrypt(pwd) // 进行加密
    return data
  },
  checkSpecificKey: function(str) {
    var specialKey = "%@[`~!#$^&*()=|{}':;',\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘'"
    for (var i = 0; i < str.length; i++) {
      if (specialKey.indexOf(str.substr(i, 1)) != -1) {
        return false
      }
    }
    return true
  },
  checkAppSpecificKey: function(str) {
    var specialKey = "%@[`~!#$^&*()=|{}':;',\\[\\]<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘'"
    for (var i = 0; i < str.length; i++) {
      if (specialKey.indexOf(str.substr(i, 1)) != -1) {
        return false
      }
    }
    return true
  },
  downloadFile: function(resBlob, fileName, fileType = '.xls', target = '_self') {
    var blob = null
    if (fileType == '.xls') {
      blob = new Blob([resBlob], {
        type: 'application/vnd.ms-excel;charset=utf-8'
      })
    } else {  // txt
      blob = new Blob([resBlob])
    }

    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      // 兼容IE/Edge
      window.navigator.msSaveOrOpenBlob(blob, fileName + fileType)
    } else {
      var url = window.URL.createObjectURL(blob)
      var a = document.createElement('a')
      a.href = url
      a.target = target
      a.style.display = 'none'
      a.setAttribute('download', fileName + fileType)
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      window.URL.revokeObjectURL(url)
    }
  }
}
